//
//  JSSeetingUserInfoVC.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/20.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSSeetingUserInfoVC.h"
#import "CuiPickerView.h"
#import "JSCountrySelectView.h"
#import "JSCityListViewController.h"
#import "JSSelectGenderView.h"
#import "JSSeetingUserIconVC.h"
#import "JSCountryInfoModel.h"
@interface JSSeetingUserInfoVC ()<CuiPickViewDelegate,JSCountrySelectViewDelegate,JSGenderSelectViewDelegate>
@property (nonatomic, strong) CuiPickerView *cuiPickerView;
@property (nonatomic, strong) JSCountrySelectView *countrySelectView;
@property (nonatomic, strong) JSSelectGenderView *genderSelectView;
@property (nonatomic, strong) NSArray *countryDataSource;
@property (nonatomic, strong) JSCountryInfoModel *currentSelectCityM;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contenHeight;
@property (weak, nonatomic) IBOutlet UILabel *birthdayL;
@property (weak, nonatomic) IBOutlet UILabel *birthdayDesL;
@property (weak, nonatomic) IBOutlet UILabel *genderL;
@property (weak, nonatomic) IBOutlet UILabel *genderDesL;
@property (weak, nonatomic) IBOutlet UILabel *countryL;
@property (weak, nonatomic) IBOutlet UILabel *countryDesL;
@property (weak, nonatomic) IBOutlet UILabel *cityL;
@property (weak, nonatomic) IBOutlet UILabel *cityDesL;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
/** locaCity*/
@property (nonatomic, strong) NSDictionary *locaCityData;

/** 性别 */
@property (nonatomic, assign) UserGender gender;

@end

@implementation JSSeetingUserInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1. 获取当前位置
    [JSNetManager js_courrentProjectRequestWithMouth:@"locationCity" cache:NO ifShowFinishStatu:NO parameters:@{}.mutableCopy successBlock:^(id response) {
       
        if ([response[@"status"] integerValue] == 200) {
             self.locaCityData = ValidDict(response[@"data"])?response[@"data"]:nil;
            if (self.locaCityData) {
                 self.currentSelectCityM = [JSCountryInfoModel mj_objectWithKeyValues:self.locaCityData];
            }
            if (JSLocalizeIsChina()) {
                self.countryDesL.text = response[@"data"][@"country_zh"];
                self.cityDesL.text    = response[@"data"][@"state_zh"];
            }else{
                self.countryDesL.text = response[@"data"][@"country"];
                self.cityDesL.text    = response[@"data"][@"state"];
            }
        }
    } failureBlock:nil];
    // 2. 获取国家列表
    [self getCity:nil];
}
- (void)getCity:(void(^)(id,int))responseData{
    [JSNetManager js_courrentProjectRequestWithMouth:@"getCity" cache:NO ifShowFinishStatu:NO parameters:@{}.mutableCopy successBlock:^(id response) {
            if ([response[@"status"] integerValue] == 200) {
                if (ValidArray(response[@"data"][@"list"])) {
                    self.countryDataSource = [JSCountryInfoModel mj_objectArrayWithKeyValuesArray:response[@"data"][@"list"]];
                }
                
                if (responseData) {
                    responseData(self.countryDataSource,200);
                }
            }else{
                if (responseData) {
                    responseData(nil,201);
                }
            }
    } failureBlock:^(NSError *error) {
        if (responseData) {
            responseData(nil,201);
        }
    }];
}
- (void)didInitialized{
    [super didInitialized];
    self.view.backgroundColor = [UIColor whiteColor];
    self.birthdayL.text = JSLocalizedString(@"birthdayLoca");
    self.genderL.text   = JSLocalizedString(@"genderLoca");
    self.countryL.text  = JSLocalizedString(@"countryLoca");
    self.cityL.text     = JSLocalizedString(@"cityLoca");
    self.genderDesL.text= JSLocalizedString(@"genderMan");
    [self.nextBtn setTitle:JSLocalizedString(kLocalizableNext) forState:UIControlStateNormal];
    self.gender = UserGenderMale;
    // 日期选择
    _cuiPickerView = [[CuiPickerView alloc]init];
    _cuiPickerView.frame = CGRectMake(0, SCREEN_HEIGHT, self.view.frame.size.width, 200);
    _cuiPickerView.delegate = self;
    _cuiPickerView.curDate=[NSDate date];
    self.birthdayDesL.text = [NSString stringWithFormat:@"%zd-%02zd-%02zd", [NSDate date].year,[NSDate date].month,[NSDate date].day];
    _genderSelectView = [[JSSelectGenderView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 168)];
    _genderSelectView.delegate = self;
    
}
- (void)setNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated{
    [super setNavigationItemsIsInEditMode:isInEditMode animated:animated];
 
    self.title = JSLocalizedString(@"userInfo");
    self.contenHeight.constant = kRealValue(44);
}
//MARL:下一步
- (IBAction)nextClick:(UIButton *)sender {
    if (!self.countryDesL.text || !self.cityDesL.text) {
        [JSAlertHelper showFailHideAfterDelay:2 text:JSLocalizedString(@"lacklocation") inView:self.view animation:YES];
        return;
    }
    self.rigsterInfo[@"birthday"] = self.birthdayDesL.text;
    self.rigsterInfo[@"sex"]      = @(self.gender == UserGenderMale?1:2);
    self.rigsterInfo[@"city_id"]  = self.currentSelectCityM.ID;
    [MBManager showLoading];
    [JSNetManager js_courrentProjectRequestWithMouth:@"setInfo" cache:NO ifShowFinishStatu:NO parameters:self.rigsterInfo successBlock:^(id response) {
        if ([response[@"status"] integerValue] == 200) {
            JSSeetingUserIconVC *vc = [[JSSeetingUserIconVC alloc] init];
            vc.parmas = self.rigsterInfo;
            if ([self.view isShowingOnKeyWindow]) {
                JS_VC_PUSHVC_ANIMATION(YES);
            }
        }
    } failureBlock:nil];
}
- (IBAction)birthdayClick:(id)sender {
    [self.cuiPickerView showMaskView];
}
- (IBAction)genderClick:(id)sender {
    [self.genderSelectView showMaskView];
}
- (IBAction)countryClick:(id)sender {
    if (!self.countryDataSource.count) {
        [MBManager showLoading];
        [self getCity:^(id data, int code) {
            if (code == 200 && [self.view isShowingOnKeyWindow]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.countrySelectView showMaskView];
                });
            }
        }];
    }else{
        [self.countrySelectView showMaskView];
    }
}
- (IBAction)cityClick:(UIButton *)sender {
    if (!self.countryDesL.text.length) {
        [JSAlertHelper showFailHideAfterDelay:2 text:JSLocalizedString(@"alterCountryLoca") inView:self.view animation:YES];
        return;
    }
    JS_WeakSelf(self)
    JSCityListViewController *vc = [[JSCityListViewController alloc] init];
    vc.title = self.countryDesL.text;
    vc.params[@"country"] = vc.title;
    vc.shouldShowSearchBar = YES;
    [vc setDidSelectCity:^(JSCountryInfoModel *cityM) {
        weakself.currentSelectCityM = cityM;
        if (JSLocalizeIsChina()) {
            weakself.cityDesL.text = cityM.state_zh;
        }else{
            weakself.cityDesL.text = cityM.state;
        }
    }];
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - CuiPickViewDelegate 时间选择回调
-(void)didFinishPickView:(NSString*)date dateStr:(NSString *)dateStr{
    JSLog(@"%@--%@--%@", dateStr,date,[NSString timestampFormatting:dateStr FormattingStyle:@"yyyy-MM-dd"]);
    self.birthdayDesL.text = [NSString timestampFormatting:dateStr FormattingStyle:@"yyyy-MM-dd"];
   
}
#pragma mark - 国籍选择回调
-(void)didFinishCountry:(NSString*)country countryM:(JSCountryInfoModel *)m{
    if (![self.countryDesL.text isEqualToString:country]) {
        self.cityDesL.text = @"";
    }
    JSLog(@"%@", m);
    self.countryDesL.text = country;
}
#pragma mark - 性别选择
-(void)didFinishGender:(NSString*)genderStr Gender:(UserGender)gender{
    self.gender = gender;
    self.genderDesL.text = genderStr;
}
- (JSCountrySelectView *)countrySelectView{
    if (!_countrySelectView) {
        _countrySelectView = [[JSCountrySelectView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 210) dataSource:self.countryDataSource];
        _countrySelectView.delegate = self;
    }
    return _countrySelectView;
}
- (NSMutableDictionary *)rigsterInfo{
    if (!_rigsterInfo) {
        _rigsterInfo = @{}.mutableCopy;
    }
    return _rigsterInfo;
}
@end


